package com.fqxiny.lease.web.app.controller.login;




import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fqxiny.lease.common.result.Result;
import com.fqxiny.lease.common.utils.JWTUtil;
import com.fqxiny.lease.model.entity.SystemUser;
import com.fqxiny.lease.model.entity.UserInfo;
import com.fqxiny.lease.web.app.service.LoginService;
import com.fqxiny.lease.web.app.service.UserInfoService;
import com.fqxiny.lease.web.app.vo.user.LoginVo;
import com.fqxiny.lease.web.app.vo.user.UserInfoVo;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "登录管理")
@RequestMapping("/app/")
public class LoginController {
    @Resource
    LoginService loginService;
    @Resource
    UserInfoService userInfoService;
    @GetMapping("login/getCode")
    @Operation(summary = "获取短信验证码")
    public Result getCode(@RequestParam String phone) {
        loginService.sendCode(phone);
        return Result.ok();
    }

    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }

    @GetMapping("info")
    @Operation(summary = "获取登录用户信息")
    public Result<UserInfoVo> info(@RequestHeader(value = "access_token" )String token) {
        Claims claims = JWTUtil.parseToken(token);
        Long userPhone = claims.get("phone", Long.class);
        UserInfo user = userInfoService.getOne(
                Wrappers.lambdaQuery(UserInfo.class)
                        .eq(UserInfo::getPhone,userPhone)
        );
        UserInfoVo uerInfoVo = new UserInfoVo(user.getNickname(),user.getAvatarUrl());
        return Result.ok(uerInfoVo);
    }
}
